const app = getApp()
import http from "../../utils/request.js";
const publicFun = require('../../utils/public');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        sysUserInfo: {}, //用户信息
        userId: {}, //用户信息
        height: 0,
        dreamId: '', //梦境id
        dream: {}, //梦境对象
        isLoveDream: false, //是否点赞
        isCollectDream: false,
        commit: '', //评论
        reply: '', //评论回复
        showReply: false, //显示回复弹窗
        commitId: '', //当前评论的id
        replyUserName: '', //当前回复评论的用户名
        replyUserId: '', //当前被回复评论的用户id
        commitList: [], //评论列表
        commitReplyList: [], //回复列表
        loading: false,
        vip: false,
    },


    //查询梦境
    getDreamDetail() {
        let data = {
            id: this.data.dreamId,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'getDreamDetail',
            method: 'GET',
            data: data
        }).then(res => {
            if (res.code == 200) {
                this.setData({
                    dream: res.data.result.length > 0 ? res.data.result[0] : {},
                    isLoveDream: res.data.loveDream, //是否点赞
                    isCollectDream: res.data.collectDream,
                });
                this.createSelectorQuery().select('#editor').context((curr) => {
                    if (!curr) return
                    curr.context.setContents({
                        html: res.data.result[0].content
                    })
                }).exec()
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 阅读梦境
    readDream() {
        let data = {
            id: this.data.dreamId,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'readDream',
            method: 'POST',
            data: data
        }).then(re => {
            if (re.code == 200) {} else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 点赞梦境
    loveDream() {
        let data = {
            id: this.data.dreamId,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'loveDream',
            method: 'POST',
            data: data
        }).then(re => {
            if (re.code == 200) {
                this.getDreamDetail();
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 收藏梦境
    collectDream() {
        let data = {
            id: this.data.dreamId,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'collectDream',
            method: 'POST',
            data: data
        }).then(re => {
            if (re.code == 200) {
                publicFun.Toast(re.msg)
                this.getDreamDetail();
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 分享梦境
    shareDream() {
        let data = {
            id: this.data.dreamId,
            userId: app.globalData.userId
        }
        http.Request({
            url: 'shareDream',
            method: 'POST',
            data: data
        }).then(re => {
            if (re.code == 200) {
                this.getDreamDetail();
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 评论
    toCommit(e) {
        this.setData({
            commit: e.detail.value
        })
    },

    // 评论清空
    clear() {
        this.setData({
            commit: ''
        })
    },

    // 评论提交
    commitSubmit(e) {
        let type = e.currentTarget.dataset.type
        let data = {
            dreamId: parseInt(this.data.dreamId),
            userId: app.globalData.userId,
            content: type == 'first' ? this.data.commit : this.data.reply,
        }
        if (!data.content) {
            publicFun.Toast('评论内容不能为空！')
            return false
        }
        if (type == 'second') {
            data.aimUserId = this.data.replyUserId
            data.aimCommitId = this.data.commitId
        }
        http.Request({
            url: 'commitDream',
            method: 'POST',
            data: data
        }).then(res => {
            if (res.code == 200) {
                publicFun.Toast(res.msg)
                this.getCommit()
                this.setData({
                    commit: '',
                    reply: '',
                    showReply: false,
                })
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    toReply(e) {
        this.setData({
            reply: e.detail.value
        })
    },

    // 评论查询
    getCommit() {
        this.setData({
            loading: true
        })
        let data = {
            dreamId: this.data.dreamId,
            userId: app.globalData.userId,
        }
        http.Request({
            url: 'getCommit',
            method: 'GET',
            data: data
        }).then(res => {
            if (res.code == 200) {
                this.setData({
                    commitList: res.data,
                    loading: false
                })
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 点赞评论
    loveCommit(e) {
        let data = {
            userId: app.globalData.userId
        }
        data.commitId = e.currentTarget.dataset.id
        http.Request({
            url: 'loveCommit',
            method: 'POST',
            data: data
        }).then(res => {
            if (res.code == 200) {
                publicFun.Toast(res.msg)
                this.getCommit()
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    //用户删除自己的评论
    commitDel(e) {
        let data = {
            commitId: e.currentTarget.dataset.id,
            dreamId: this.data.dreamId
        }
        http.Request({
            url: 'delCommit',
            method: 'POST',
            data: data
        }).then(res => {
            if (res.code == 200) {
                publicFun.Toast(res.msg)
                this.getCommit()
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    // 显示回复弹框
    showModal(e) {
        this.setData({
            commitId: e.currentTarget.dataset.commitid,
            replyUserName: e.currentTarget.dataset.name,
            replyUserId: e.currentTarget.dataset.userid,
            showReply: true,
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            dreamId: options.id,
            height: wx.getSystemInfoSync().windowHeight,
            sysUserInfo: app.globalData.sysUserInfo,
            userId: app.globalData.userId,
            vip: app.globalData.vip
        })
        this.readDream();
        this.getCommit();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getDreamDetail();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let that = this;
        return {
            title: '梦萦记',
            path: 'page/dreamDetail/dreamDetail?id=' + this.data.dreamId,
            success(res) {
                that.shareDream();
            }
        }
    }
})